package car_search

import (
	"car-thirdparty-api/internal/consts"
	"car-thirdparty-api/internal/dao"
	"car-thirdparty-api/internal/model"
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gtime"

	"car-thirdparty-api/api/car_search/v1"
)

func (c *ControllerV1) AppCarSearch(ctx context.Context, req *v1.AppCarSearchReq) (res *v1.AppCarSearchRes, err error) {
	res = new(v1.AppCarSearchRes)
	resData := c.queryAppCard(ctx, req.Plate)
	if resData != nil {
		res.CarSearchItem = resData
		goto end
	}

	resData = c.queryAppCardOnline(ctx, req.Plate)
	if resData != nil {
		res.CarSearchItem = resData
		goto end
	}

	resData = c.queryAppOrder(ctx, req.Plate)
	if resData != nil {
		res.CarSearchItem = resData
		goto end
	}

	resData = c.queryAppBlackList(ctx, req.Plate)
	if resData != nil {
		res.CarSearchItem = resData
		goto end
	}

end:
	if resData == nil {
		res = nil
	}
	return
}

func (c *ControllerV1) queryAppCard(ctx context.Context, plate string) (resData *model.CarSearchItem) {
	var card *model.CardSearch

	err := dao.Card.Ctx(ctx).
		Where(dao.Card.Columns().CarNum, plate).
		WhereNotIn(dao.Card.Columns().Flag, []int{3, 13}).
		Where(dao.Card.Columns().StartTime+" <= ?", gtime.Now()).
		Where(dao.Card.Columns().EndTime+" >= ?", gtime.Now()).
		Limit(1).
		Scan(&card)
	if err != nil {
		g.Log().Error(ctx, "查询办证信息失败, ", err)
	}

	if card != nil {
		// 动态关联绑定
		err = g.Model("sys_user").Scan(&card.BUser, "user_name", card.Card.JobId)
		if err != nil {
			return nil
		}

		err = g.Model("sys_user").Scan(&card.CUser, "user_name", card.Card.CreateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_user").Scan(&card.UUser, "user_name", card.Card.UpdateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_dept").Scan(&card.Dept, "dept_id", card.Card.DeptId)
		if err != nil {
			return nil
		}

		resData = &model.CarSearchItem{
			CarNum:     card.CarNum,
			DataType:   consts.Card,
			Identity:   card.CarId,
			Name:       card.CardName,
			PhoneNum:   card.CardTel,
			JobId:      card.JobId,
			StartTime:  card.StartTime.Format(consts.DateFormat),
			EndTime:    card.EndTime.Format(consts.DateFormat),
			ExpireTime: "",
		}
		if card.BUser != nil {
			resData.BUserName = card.BUser.NickName
		}
		if card.CUser != nil {
			resData.CUserName = card.CUser.NickName
		}
		if card.UUser != nil {
			resData.UUserName = card.UUser.NickName
		}
		if card.Dept != nil {
			resData.DeptName = card.Dept.DeptName
		}
	}

	return
}

// 查询月租车信息表
func (c *ControllerV1) queryAppCardOnline(ctx context.Context, plate string) (resData *model.CarSearchItem) {
	var cardOnline *model.CardOnlineSearch
	err := dao.CardOnline.Ctx(ctx).
		Where(dao.CardOnline.Columns().CarNum, plate).
		Where(dao.CardOnline.Columns().StartTime+" <= ?", gtime.Now()).
		Where(dao.CardOnline.Columns().EndTime+" >= ?", gtime.Now()).
		Limit(1).
		Scan(&cardOnline)
	if err != nil {
		g.Log().Error(ctx, "查询月租车信息失败, ", err)
	}

	if cardOnline != nil {
		// 动态关联绑定
		err = g.Model("sys_user").Scan(&cardOnline.CUser, "user_name", cardOnline.CardOnline.CreateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_user").Scan(&cardOnline.UUser, "user_name", cardOnline.CardOnline.UpdateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_dept").Scan(&cardOnline.Dept, "dept_id", cardOnline.CardOnline.DeptId)
		if err != nil {
			return nil
		}

		resData = &model.CarSearchItem{
			CarNum:     cardOnline.CarNum,
			DataType:   consts.CardOnline,
			Identity:   cardOnline.CardIdNum,
			Name:       cardOnline.CardName,
			PhoneNum:   cardOnline.CardTel,
			StartTime:  cardOnline.StartTime.Format(consts.DateFormat),
			EndTime:    cardOnline.EndTime.Format(consts.DateFormat),
			ExpireTime: cardOnline.ExpDate.Format(consts.DateFormat),
		}
		if cardOnline.CUser != nil {
			resData.CUserName = cardOnline.CUser.NickName
		}
		if cardOnline.UUser != nil {
			resData.UUserName = cardOnline.UUser.NickName
		}
		if cardOnline.Dept != nil {
			resData.DeptName = cardOnline.Dept.DeptName
		}
	}
	return
}

// 查询预约车信息表
func (c *ControllerV1) queryAppOrder(ctx context.Context, plate string) (resData *model.CarSearchItem) {
	var bookingCard *model.BookingCardSearch
	err := dao.BookingCard.Ctx(ctx).
		Where(dao.BookingCard.Columns().CarNum, plate).
		Where(dao.BookingCard.Columns().OrderStartTime+" <= ?", gtime.Now()).
		Where(dao.BookingCard.Columns().OrderEndTime+" >= ?", gtime.Now()).
		OrderDesc(dao.BookingCard.Columns().CreateTime).
		Limit(1).
		Scan(&bookingCard)
	if err != nil {
		g.Log().Error(ctx, "查询预约车信息失败, ", err)
	}

	if bookingCard != nil {
		// 动态关联查询人员信息和部门信息
		err = g.Model("sys_user").Scan(&bookingCard.CUser, "user_name", bookingCard.BookingCard.CreateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_user").Scan(&bookingCard.UUser, "user_name", bookingCard.BookingCard.UpdateBy)
		if err != nil {
			return nil
		}

		err = g.Model("sys_dept").Scan(&bookingCard.Dept, "dept_id", bookingCard.BookingCard.DeptId)
		if err != nil {
			return nil
		}

		resData = &model.CarSearchItem{
			CarNum:     bookingCard.CarNum,
			DataType:   consts.BookingCard,
			Identity:   "",
			Name:       bookingCard.OrderUser,
			PhoneNum:   bookingCard.OrderTel,
			StartTime:  bookingCard.OrderStartTime.Format(consts.DateFormat),
			EndTime:    bookingCard.OrderEndTime.Format(consts.DateFormat),
			ExpireTime: "",
		}
		if bookingCard.CUser != nil {
			resData.CUserName = bookingCard.CUser.NickName
		}
		if bookingCard.UUser != nil {
			resData.UUserName = bookingCard.UUser.NickName
		}
		if bookingCard.Dept != nil {
			resData.DeptName = bookingCard.Dept.DeptName
		}
	}

	return
}

// 查询黑名单信息表
func (c *ControllerV1) queryAppBlackList(ctx context.Context, plate string) (resData *model.CarSearchItem) {
	var blackList *model.BlackListSearch
	err := dao.BlackListRecord.Ctx(ctx).
		Where(dao.BlackListRecord.Columns().CarNum, plate).
		OrderDesc(dao.BlackListRecord.Columns().CreateTime).
		Limit(1).
		Scan(&blackList)
	if err != nil {
		g.Log().Error(ctx, "查询黑名单信息失败, ", err)
	}

	if blackList != nil {
		// 动态关联查询人员信息和部门信息
		err = g.Model("sys_user").Scan(&blackList.CUser, "user_name", blackList.BlackListRecord.CreateBy)
		if err != nil {
			return nil
		}

		resData = &model.CarSearchItem{
			CarNum:   blackList.CarNum,
			DataType: consts.BlackList,
			Identity: "",
			Name:     "",
			PhoneNum: "",
		}
		if blackList.CUser != nil {
			resData.CUserName = blackList.CUser.NickName
		}
	}
	return
}
